﻿var timePassed = function(unixTime) {

    var pluralize = function(amt) {
        if (amt >= 1 && amt < 2) {
            return "";
        }
        else {
            return "s";
        }
    }

    if (typeof unixTime === "Date") {
        unixTime = unixTime.getTime();
    }

    var ms = new Date().getTime() - unixTime;

    if ((secs = ms / 1000) < 1) {
        amount = ms + " ms";
    }
    else if ((mins = secs / 60) < 1) {
        amount = secs.toFixed(2) + " sec" + pluralize(secs);
    }
    else if ((hours = mins / 60) < 1) {
        amount = Math.floor(mins) + " min" + pluralize(mins);
    }
    else if ((days = hours / 24) < 1) {
        amount = Math.floor(hours) + " hour" + pluralize(hours);
    }
    else if ((weeks = days / 7) < 1) {
        amount = Math.floor(days) + " day" + pluralize(days);
    }
    else if ((months = days / 30) < 1) {
        amount = Math.floor(weeks) + " week" + pluralize(weeks);
    }
    else if ((years = days / 365) < 1) {
        amount = Math.floor(months) + " month" + pluralize(months);
    }
    else {
        amount = Math.floor(years) + " year" + pluralize(years);
    }

    return amount + " ago";
};


